//
//  thanksViewController.m
//  INAlienLand
//
//  Created by dllo on 15/7/3.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "thanksViewController.h"
#import "HTTPTool.h"
#import "CityListModel.h"
#import "thanksCollectionViewCell.h"
#import "UIImageView+WebCache.h"


@interface thanksViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,retain)NSMutableArray *cityArr;
@property (nonatomic,retain)UICollectionView *cityCollect;

@end

@implementation thanksViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.941 green:1.000 blue:0.393 alpha:1.000];
    [self createData];
    [self createCollection];
}
-(void)createCollection;
{
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize = CGSizeMake((self.view.frame.size.width-40)/2, (self.view.frame.size.height-150)/2);
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.cityCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64-49) collectionViewLayout:flowlayout];
    [self.view addSubview:self.cityCollect];
    self.cityCollect.backgroundColor = [UIColor colorWithRed:0.960 green:0.968 blue:1.000 alpha:1.000];
    self.cityCollect.delegate = self;
    self.cityCollect.dataSource = self;
    [self.cityCollect registerClass:[thanksCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.cityArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    thanksCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    CityListModel *city = self.cityArr[indexPath.row];
    [cell.cityImage sd_setImageWithURL:[NSURL URLWithString:city.cover_route_map_cover]];
    cell.nameLable.text = city.name;
    
    return cell;
}
-(void)createData
{
    self.cityArr = [NSMutableArray array];
  [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%@/",self.index] body:nil HTTPResult:JSON success:^(id result) {
      NSDictionary *dic = result;
      NSArray *listName = dic[@"data"];
      for (NSDictionary *tempDic in listName) {
          CityListModel *city = [[CityListModel alloc]init];
          [city setValuesForKeysWithDictionary:tempDic];
          [self.cityArr addObject:city];
         
          
      }
      [self.cityCollect reloadData];
  } failure:^(NSError *error) {
      
  }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
